﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Person.Classes;
using System.IO;

namespace Person.DS
{

    public class DS_CSV : IPerson
    {
        private string path;
        public string Path { set { path = value; } }
        private IPerson next = null;
        public IPerson Next { set { next = value; } }
        public DS_CSV() { }
        public DS_CSV(string _path) { Path = _path; }
        public IPerson isReady(string _ext, string _path)
        {
            IPerson ret = null;
            if ("csv" == _ext) { ret = this; ret.Path = _path; }
            else if (next != null) { ret = next.isReady(_ext, _path); }
            return ret;
        }

        public void Create(Persona p)
        {
            if ((path != "") && (File.Exists(path)))
            {
                StreamWriter f = new StreamWriter(path, true);
                f.WriteLine(Persona.MakeCSVStrFromPersona(p));
                f.Close();
            }
            else
            {
                throw new System.Exception();
            }
        }
        public List<Persona> Read()
        {
            List<Persona> list = new List<Persona>();
            if ((path != "") && (File.Exists(path)))
            {
                StreamReader reader = new StreamReader(path);
                string str;
                while ((str = reader.ReadLine()) != null)
                {
                    Persona per = Persona.GetPersonaFromCSVStr(str);
                    list.Add(per);
                }
                reader.Close();

                return list;
            }
            else
            {
                throw new System.Exception();
            }
        }
        public List<Persona> Read(int ID)
        {
            List<Persona> l = Read();
            List<Persona> list = new List<Persona>();
            foreach (Persona p in l)
            {
                if (p.ID == ID)
                {
                    list.Add(p);
                }
            }

            return list;
        }
        public void Update(Persona p)
        {
            string path2 = path.Substring(0,path.IndexOf('.'))+"New.csv";
            if ((path != "") && (File.Exists(path)))
            {
                StreamReader reader = new StreamReader(path);
                string str;
                while ((str = reader.ReadLine()) != null)
                {
                    Persona per = Persona.GetPersonaFromCSVStr(str);
                    if (per.ID == p.ID)
                    {
                        per.Fname = p.Fname;
                        per.Lname = p.Lname;
                        per.Age = p.Age;
                    }
                    StreamWriter writer = new StreamWriter(path2, true);
                    writer.WriteLine(Persona.MakeCSVStrFromPersona(per));
                    writer.Close();
                }
                reader.Close();
                File.Delete(path);
                File.Move(path2, path);
            }
            else
            {
                throw new System.Exception();
            }            
        }
        public void Delete(int ID)
        {
            string path2 = path.Substring(0, path.IndexOf('.')) + "New.csv";
            if ((path != "") && (File.Exists(path)))
            {
                StreamReader reader = new StreamReader(path);
                string str;
                while ((str = reader.ReadLine()) != null)
                {
                    Persona per = Persona.GetPersonaFromCSVStr(str);
                    if (per.ID != ID)
                    {
                        StreamWriter writer = new StreamWriter(path2, true);
                        writer.WriteLine(Persona.MakeCSVStrFromPersona(per));
                        writer.Close();
                    }
                }
                reader.Close();
                File.Delete(path);
                File.Move(path2, path);
            }
            else
            {
                throw new System.Exception();
            }  

        }
       

    }
}
